package gat.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @Author：liucaihong
 * @date：2023/4/20 10:45
 * @description:
 */
@Slf4j
public class CommonUtil {

    /**
     * @param parameterMap
     * @return: java.util.Map<java.lang.String, java.lang.String>
     * @Author: liucaihong
     * @date: 2023-04-20 10:48:28
     * @Description: Map<String, String[]> 转 Map<String, String>
     */
    public static Map<String, Object> convertMap(Map<String, String[]> parameterMap) {
        Map<String, Object> returnMap = new HashMap<>();
        // 转换为Entry
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        // 遍历
        for (Map.Entry<String, String[]> entry : entries) {
            String key = entry.getKey();
            StringBuilder value = new StringBuilder("");
            String[] val = entry.getValue();
            if (null != val && val.length > 0) {
                for (String v:val) {
                    value.append(v);
                }
            }
            returnMap.put(key, value);
        }
        return returnMap;
    }

    /**
     * @param map
     * @return: java.lang.String
     * @Author: liucaihong
     * @date: 2023-04-20 10:57:24
     * @Description: 将参数进行ASCII排序后，采用&进行拼接
     */
    public static String ascllSortAndJoint(Map<String,Object> map){
        // 移除值为空的
        map.entrySet().removeIf(entry -> StringUtils.isEmpty(entry.getValue()));
        List<Map.Entry<String, Object>> infoIds = new ArrayList<>(map.size());
        infoIds.addAll(map.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
        infoIds.sort((o1, o2) -> o1.getKey().compareToIgnoreCase(o2.getKey()));
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> infoId : infoIds) {
            sb.append(infoId.getKey());
            sb.append("=");
            sb.append(infoId.getValue());
            sb.append("&");
        }
        return sb.substring(0, sb.length() - 1);
    }


    public static void main(String[] args) {
        String s = "abcd";


    }

    public static boolean isValidIdCard(String idCard) {
        // 步骤1：检查长度
        if (idCard.length() == 18) {
            // 步骤2：使用正则表达式验证格式
            if (idCard.matches("^\\d{17}[\\dXx]$")) {
                // 步骤3：最后一位的校验码检查
                int[] weights = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                int checkSum = 0;
                for (int i = 0; i < 17; i++) {
                    checkSum += Character.getNumericValue(idCard.charAt(i)) * weights[i];
                }
                int checkCode = (12 - checkSum % 11) % 11;
                char lastChar = idCard.charAt(17);
                char expectedChar = (checkCode == 10) ? 'X' : (char) (checkCode + '0');

                return Character.toUpperCase(lastChar) == expectedChar;
            }
        } else if (idCard.length() == 15) {
            // 15位身份证号不包含校验码，直接使用正则表达式验证格式
            return idCard.matches("^\\d{15}$");
        }

        return false;
    }
}
